/**
 * 
 */
package org.istt.web;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletContext;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.lang.time.DateUtils;
import org.apache.struts2.ServletActionContext;
import org.istt.domain.FileBoSungCCHN;
import org.istt.domain.FileCapCCHN;
import org.istt.domain.FileCapCCMGBDS;
import org.istt.domain.FileCapLaiCCHN;
import org.istt.domain.FileCapPhepXayDung;
import org.istt.domain.FileDieuChinhGPXD;
import org.istt.domain.FileGiaHanCPXD;
import org.istt.domain.TimeHoliday;
import org.istt.domain.TimeWeekend;
import org.istt.export.BmQT_CCDG_CMCL;
import org.istt.export.BmQT_CCHN_BS;
import org.istt.export.BmQT_CCHN_CL;
import org.istt.export.BmQT_CCHN_CM;
import org.istt.export.BmQT_GPXD_CM;
import org.istt.export.BmQT_GPXD_DC;
import org.istt.export.BmQT_GPXD_GH;
import org.istt.export.BmQT_Notify;
import org.istt.export.ExportFile;
import org.istt.export.ProcessingReportCreator;
import org.istt.export.ReceiptCreator;
import org.istt.export.ReportProcessingFiles;
import org.istt.export.ReportSynthesis;
import org.istt.model.FileReportBean;
import org.istt.model.GeneralReportBean;
import org.istt.model.NotificationBean;
import org.istt.service.FileBoSungCCHNManager;
import org.istt.service.FileCapCCHNManager;
import org.istt.service.FileCapCCMGBDSManager;
import org.istt.service.FileCapGPXDManager;
import org.istt.service.FileCapLaiCCHNManager;
import org.istt.service.FileDieuChinhGPXDManager;
import org.istt.service.FileGiaHanGPXDManager;
import org.istt.service.FileManager;
import org.istt.service.TimeHolidayManager;
import org.istt.service.TimeWeekendManager;
import org.joda.time.DateMidnight;
import org.joda.time.DateTime;
import org.joda.time.DateTimeConstants;
import org.joda.time.MutableDateTime;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;

/**
 * @author Huy
 *
 */
public class ReceiptAction extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = -8860322902844493259L;
	String documentFormat = "doc";
	private FileBoSungCCHNManager fileBoSungCCHNMgr;
	private FileCapCCHNManager fileCapCCHNMgr;
	private FileCapCCMGBDSManager fileCapCCMGBDSMgr;
	
	private FileCapLaiCCHNManager fileCapLaiCCHNMgr;
	
	private FileCapGPXDManager fileCapPhepXDMgr;
	
	private FileDieuChinhGPXDManager fileDieuChinhGPXDMgr;
	
	private FileGiaHanGPXDManager fileGiaHanCPXDMgr;
	
	private Long formId;
	
	private String id;
	
	@Autowired
	private FileManager manager;
	
	private Long type;

	private Integer formtype;
	
	
	
	InputStream wordStream;
	
	public String exportCCDG_CL() throws IOException {
		FileCapCCMGBDS fileCapCCDG = fileCapCCMGBDSMgr.get(formId);
		
		wordStream = new BmQT_CCDG_CMCL().toMSWord(getFile("/word/CCDG_CL.docx"),fileCapCCDG);
		
		return SUCCESS;
	}
	public String exportCCDG_CM() throws IOException {
		FileCapCCMGBDS fileCapCCDG = fileCapCCMGBDSMgr.get(formId);
		
		wordStream = new BmQT_CCDG_CMCL().toMSWord(getFile("/word/CCDG_CM.docx"),fileCapCCDG);
		
		return SUCCESS;
	}
	
	public String exportCCHN_BS() throws IOException {
		FileBoSungCCHN fileBoSungCCHN = fileBoSungCCHNMgr.get(formId);
		
		wordStream = new BmQT_CCHN_BS().toMSWord(getFile("/word/CCHN_BS.docx"),fileBoSungCCHN);
		
		return SUCCESS;
	}
	
	public String exportCCHN_CL() throws IOException {
		FileCapLaiCCHN fileCapLaiCCHN = fileCapLaiCCHNMgr.get(formId);
		
		wordStream = new BmQT_CCHN_CL().toMSWord(getFile("/word/CCHN_CL.docx"),fileCapLaiCCHN);
		
		return SUCCESS;
	}

	public String exportCCHN_CM() throws IOException {
		FileCapCCHN fileCapCCHN = fileCapCCHNMgr.get(formId);
		
		wordStream = new BmQT_CCHN_CM().toMSWord(getFile("/word/CCHN_CM.docx"),fileCapCCHN);
		
		return SUCCESS;
	}
	public String exportCCMG_CL() throws IOException {
		FileCapCCMGBDS fileCapCCDG = fileCapCCMGBDSMgr.get(formId);
		
		wordStream = new BmQT_CCDG_CMCL().toMSWord(getFile("/word/CCMG_CL.docx"),fileCapCCDG);
		
		return SUCCESS;
	}

	public String exportCCMG_CM() throws IOException {
		FileCapCCMGBDS fileCapCCDG = fileCapCCMGBDSMgr.get(formId);
		
		wordStream = new BmQT_CCDG_CMCL().toMSWord(getFile("/word/CCMG_CM.docx"),fileCapCCDG);
		
		return SUCCESS;
	}

	public String exportGPXD_CM() throws IOException {
		FileCapPhepXayDung fileCapPhepXayDung = fileCapPhepXDMgr.get(formId);
		
		wordStream = new BmQT_GPXD_CM().toMSWord(getFile("/word/GPXD_CM.docx"),fileCapPhepXayDung);

		return SUCCESS;
	}
	
	public String exportGPXD_DC() throws IOException {
		FileDieuChinhGPXD fileDieuChinhGPXD = fileDieuChinhGPXDMgr.get(formId);
		
		wordStream = new BmQT_GPXD_DC().toMSWord(getFile("/word/GPXD_DC.docx"),fileDieuChinhGPXD);
		
		return SUCCESS;
	}
	
	public String exportGPXD_GH() throws IOException {
		FileGiaHanCPXD fileGiaHanCPXD = fileGiaHanCPXDMgr.get(formId);
		
		wordStream = new BmQT_GPXD_GH().toMSWord(getFile("/word/GPXD_GH.docx"),fileGiaHanCPXD);
		
		return SUCCESS;
	}
	
	/**
	 * Export list files of a department to word
	 * @return
	 * @throws IOException
	 */
	public String exportLstFiles() throws IOException {
		ServletContext servletContext = ServletActionContext.getServletContext();
		@SuppressWarnings("unchecked")
		List<FileReportBean> lstFiles = (List<FileReportBean>)servletContext.getAttribute(WebConstants.SERVLET_CONTEXT_REPORT);
		String fromdate = (String)servletContext.getAttribute(WebConstants.SERVLET_CONTEXT_FROMDATE);
		String todate = (String)servletContext.getAttribute(WebConstants.SERVLET_CONTEXT_TODATE);
		String reportType = (String)servletContext.getAttribute(WebConstants.REPORT_TYPE);
		
		
		wordStream = new ReportProcessingFiles().toMSWord(getFile("/word/BC_DS_HO_SO_THEO_PHONG.docx"),lstFiles,reportType ,fromdate,todate);
		return SUCCESS;
	}

	public String getDocumentLocation(){
    	ServletContext servletContext = ServletActionContext.getServletContext();
    	String dirPaths = servletContext.getRealPath("");
    	return dirPaths.substring(0, dirPaths.lastIndexOf("\\"))+"\\template\\";
    }
	
	public String exportReceiptFile() throws IOException {
		String dirPath = getDocumentLocation();
		org.istt.domain.File document = getObjFile();
		
		InputStream is = new FileInputStream(new File(dirPath+document.getCategory().getReport().getFunctionName()));
		wordStream = new ReceiptCreator().toMSWord(is,document,((FileManager) manager).findAttachFileDetailByFileId(id.toString()));
		return SUCCESS;
	}
	
	public String exportProcessingReport() throws IOException {
		String dirPath = getDocumentLocation();
		org.istt.domain.File document = getObjFile();
		
		InputStream is = new FileInputStream(new File(dirPath+document.getCategory().getVoucher().getFunctionName()));
		wordStream = new ProcessingReportCreator().toMSWord(is,document,getFileProcessVoucher());
		return SUCCESS;
	}
	
	/*public String exportReceiptFile() throws IOException {
		wordStream = new ReceiptCreator().toMSWord(getFile("/word/EXAMPLE.docx"),getObjFile(),((FileManager) manager).findAttachFileDetailByFileId(id.toString()));
		return SUCCESS;
	}*/

	public String getDocument() throws IOException {
		FileCapCCMGBDS fileCapCCMGBDS = null;
		if(this.formId!=null){
				switch(formtype)
				{
				  case 1:
					  FileCapPhepXayDung fileCapPhepXayDung = fileCapPhepXDMgr.get(formId);
					  wordStream = new BmQT_GPXD_CM().toMSWord(getFile("/word/GPXD_CM.docx"),fileCapPhepXayDung);
					  break;			
					  
				  case 2:
					  FileCapCCHN fileCapCCHN = fileCapCCHNMgr.get(formId);
					  wordStream = new BmQT_CCHN_CM().toMSWord(getFile("/word/CCHN_CM.docx"),fileCapCCHN);
					  break;
					  
				  case 3:
					  FileCapLaiCCHN fileCapLaiCCHN = fileCapLaiCCHNMgr.get(formId);
					  wordStream = new BmQT_CCHN_CL().toMSWord(getFile("/word/CCHN_CL.docx"),fileCapLaiCCHN);
					  break;
					  
				  case 4:
					  FileDieuChinhGPXD fileDieuChinhGPXD = fileDieuChinhGPXDMgr.get(formId);
					  wordStream = new BmQT_GPXD_DC().toMSWord(getFile("/word/GPXD_DC.docx"),fileDieuChinhGPXD);
					  break;
					  
				  case 5:
					  FileGiaHanCPXD fileGiaHanCPXD = fileGiaHanCPXDMgr.get(formId);
					  wordStream = new BmQT_GPXD_GH().toMSWord(getFile("/word/GPXD_GH.docx"),fileGiaHanCPXD);
					  break;
					  
				  case 6:
					  FileBoSungCCHN fileBoSungCCHN = fileBoSungCCHNMgr.get(formId);
					  wordStream = new BmQT_CCHN_BS().toMSWord(getFile("/word/CCHN_BS.docx"),fileBoSungCCHN);
					  break;
					  
				  case 7: // Cap moi Chung chi moi gioi BDS
					  fileCapCCMGBDS = fileCapCCMGBDSMgr.get(formId);
					  wordStream = new BmQT_CCDG_CMCL().toMSWord(getFile("/word/CCMG_CM.docx"),fileCapCCMGBDS);
					  break;
				  case 8: // Cap lai Chung chi moi gioi BDS
					  fileCapCCMGBDS = fileCapCCMGBDSMgr.get(formId);
					  wordStream = new BmQT_CCDG_CMCL().toMSWord(getFile("/word/CCMG_CL.docx"),fileCapCCMGBDS);
					  break;
				  case 9: // Cap moi chung chi dinh gia BDS
					  fileCapCCMGBDS = fileCapCCMGBDSMgr.get(formId);
					  wordStream = new BmQT_CCDG_CMCL().toMSWord(getFile("/word/CCDG_CM.docx"),fileCapCCMGBDS);
					  break;
					  
				  case 10: // cap lai chung chi dinh gia BDS
					  fileCapCCMGBDS = fileCapCCMGBDSMgr.get(formId);
					  wordStream = new BmQT_CCDG_CMCL().toMSWord(getFile("/word/CCDG_CL.docx"),fileCapCCMGBDS);
					  break;
				}
		}
		return SUCCESS;
	}
	public String exportNotify() throws IOException {
		NotificationBean bean = new NotificationBean();
		FileCapCCMGBDS fileCapCCMGBDS = new FileCapCCMGBDS();
		if(this.formId!=null){
			switch(formtype)
			{
			  case 1:
				  FileCapPhepXayDung fileCapPhepXayDung = fileCapPhepXDMgr.get(formId);
				  if(fileCapPhepXayDung!=null){
					  bean.setContent(getText("online.formType.name.capGPXD")
							  +getText(", m\u00E3 s\u1ED1 ")
							  + fileCapPhepXayDung.getFileCode()
							  + getText(", c\u1EE7a ")
							  + fileCapPhepXayDung.getTen_lien_he()
							  + getText(" \u0111\u00E3 \u0111\u01B0\u1EE3c g\u1EDFi \u0111\u1EBFn S\u1EDF x\u00E2y d\u1EF1ng B\u00ECnh D\u01B0\u01A1ng. Ch\u00FAng t\u00F4i s\u1EBD ki\u1EC3m tra h\u1ED3 s\u01A1 v\u00E0 th\u00F4ng b\u00E1o k\u1EBFt qu\u1EA3 theo \u0111\u1ECBa ch\u1EC9 th\u01B0 \u0111i\u1EC7n t\u1EED ")
							  + fileCapPhepXayDung.getEmail()
							  + getText(" ho\u1EB7c s\u1ED1 \u0111i\u1EC7n tho\u1EA1i ")
							  + fileCapPhepXayDung.getDien_thoai());
				  }
				  break;			
				  
			  case 2:
				  FileCapCCHN fileCapCCHN = fileCapCCHNMgr.get(formId);
				  if(fileCapCCHN!=null){
					  bean.setContent(getText("online.formType.name.capCCHN")
							  +getText(", m\u00E3 s\u1ED1 ")
							  + fileCapCCHN.getFileCode()
							  + getText(", c\u1EE7a ")
							  + fileCapCCHN.getTen_lien_he()
							  + getText(" \u0111\u00E3 \u0111\u01B0\u1EE3c g\u1EDFi \u0111\u1EBFn S\u1EDF x\u00E2y d\u1EF1ng B\u00ECnh D\u01B0\u01A1ng. Ch\u00FAng t\u00F4i s\u1EBD ki\u1EC3m tra h\u1ED3 s\u01A1 v\u00E0 th\u00F4ng b\u00E1o k\u1EBFt qu\u1EA3 theo \u0111\u1ECBa ch\u1EC9 th\u01B0 \u0111i\u1EC7n t\u1EED ")
							  + fileCapCCHN.getEmail()
							  + getText(" ho\u1EB7c s\u1ED1 \u0111i\u1EC7n tho\u1EA1i "));
				  }
				  break;
				  
			  case 3:
				  FileCapLaiCCHN fileCapLaiCCHN = fileCapLaiCCHNMgr.get(formId);
				  if(fileCapLaiCCHN!=null){
					  bean.setContent(getText("online.formType.name.capLaiCCHN")
							  +getText(", m\u00E3 s\u1ED1 ")
							  + fileCapLaiCCHN.getFileCode()
							  + getText(", c\u1EE7a ")
							  + fileCapLaiCCHN.getContactName()
							  + getText(" \u0111\u00E3 \u0111\u01B0\u1EE3c g\u1EDFi \u0111\u1EBFn S\u1EDF x\u00E2y d\u1EF1ng B\u00ECnh D\u01B0\u01A1ng. Ch\u00FAng t\u00F4i s\u1EBD ki\u1EC3m tra h\u1ED3 s\u01A1 v\u00E0 th\u00F4ng b\u00E1o k\u1EBFt qu\u1EA3 theo \u0111\u1ECBa ch\u1EC9 th\u01B0 \u0111i\u1EC7n t\u1EED ")
							  + fileCapLaiCCHN.getEmail()
							  + getText(" ho\u1EB7c s\u1ED1 \u0111i\u1EC7n tho\u1EA1i "));
				  }
				  break;
				  
			  case 4:
				  FileDieuChinhGPXD fileDieuChinhGPXD = fileDieuChinhGPXDMgr.get(formId);
				  if(fileDieuChinhGPXD!=null){
					  bean.setContent(getText("online.formType.name.dieuChinh_GPXD")
							  +getText(", m\u00E3 s\u1ED1 ")
							  + fileDieuChinhGPXD.getFileCode()
							  + getText(", c\u1EE7a ")
							  + fileDieuChinhGPXD.getTen_lien_he()
							  + getText(" \u0111\u00E3 \u0111\u01B0\u1EE3c g\u1EDFi \u0111\u1EBFn S\u1EDF x\u00E2y d\u1EF1ng B\u00ECnh D\u01B0\u01A1ng. Ch\u00FAng t\u00F4i s\u1EBD ki\u1EC3m tra h\u1ED3 s\u01A1 v\u00E0 th\u00F4ng b\u00E1o k\u1EBFt qu\u1EA3 theo \u0111\u1ECBa ch\u1EC9 th\u01B0 \u0111i\u1EC7n t\u1EED ")
							  + fileDieuChinhGPXD.getEmail()
							  + getText(" ho\u1EB7c s\u1ED1 \u0111i\u1EC7n tho\u1EA1i ")
							  + fileDieuChinhGPXD.getDien_thoai());
				  }
				  break;
				  
			  case 5:
				  FileGiaHanCPXD fileGiaHanCPXD = fileGiaHanCPXDMgr.get(formId);
				  if(fileGiaHanCPXD!=null){
					  bean.setContent(getText("online.formType.name.capGiaHan_GPXD")
							  +getText(", m\u00E3 s\u1ED1 ")
							  + fileGiaHanCPXD.getFileCode()
							  + getText(", c\u1EE7a ")
							  + fileGiaHanCPXD.getTen_lien_he()
							  + getText(" \u0111\u00E3 \u0111\u01B0\u1EE3c g\u1EDFi \u0111\u1EBFn S\u1EDF x\u00E2y d\u1EF1ng B\u00ECnh D\u01B0\u01A1ng. Ch\u00FAng t\u00F4i s\u1EBD ki\u1EC3m tra h\u1ED3 s\u01A1 v\u00E0 th\u00F4ng b\u00E1o k\u1EBFt qu\u1EA3 theo \u0111\u1ECBa ch\u1EC9 th\u01B0 \u0111i\u1EC7n t\u1EED ")
							  + fileGiaHanCPXD.getEmail()
							  + getText(" ho\u1EB7c s\u1ED1 \u0111i\u1EC7n tho\u1EA1i ")
							  + fileGiaHanCPXD.getDien_thoai());
				  }
				  break;
				  
			  case 6:
				  FileBoSungCCHN fileBoSungCCHN = fileBoSungCCHNMgr.get(formId);
				  if(fileBoSungCCHN!=null){
					  bean.setContent(getText("online.formType.name.capBoSungCCHN")
							  +getText(", m\u00E3 s\u1ED1 ")
							  + fileBoSungCCHN.getFileCode()
							  + getText(", c\u1EE7a ")
							  + fileBoSungCCHN.getTen_lien_he()
							  + getText(" \u0111\u00E3 \u0111\u01B0\u1EE3c g\u1EDFi \u0111\u1EBFn S\u1EDF x\u00E2y d\u1EF1ng B\u00ECnh D\u01B0\u01A1ng. Ch\u00FAng t\u00F4i s\u1EBD ki\u1EC3m tra h\u1ED3 s\u01A1 v\u00E0 th\u00F4ng b\u00E1o k\u1EBFt qu\u1EA3 theo \u0111\u1ECBa ch\u1EC9 th\u01B0 \u0111i\u1EC7n t\u1EED ")
							  + fileBoSungCCHN.getEmail()
							  + getText(" ho\u1EB7c s\u1ED1 \u0111i\u1EC7n tho\u1EA1i "));
				  }
				  break;
				  
			  case 7: // Cap moi Chung chi moi gioi BDS
				  fileCapCCMGBDS = fileCapCCMGBDSMgr.get(formId);
				  if(fileCapCCMGBDS!=null){
					  bean.setContent(getText("online.formType.name.capBoSungCCHN")
							  +getText(", m\u00E3 s\u1ED1 ")
							  + fileCapCCMGBDS.getFileCode()
							  + getText(", c\u1EE7a ")
							  + fileCapCCMGBDS.getContactName()
							  + getText(" \u0111\u00E3 \u0111\u01B0\u1EE3c g\u1EDFi \u0111\u1EBFn S\u1EDF x\u00E2y d\u1EF1ng B\u00ECnh D\u01B0\u01A1ng. Ch\u00FAng t\u00F4i s\u1EBD ki\u1EC3m tra h\u1ED3 s\u01A1 v\u00E0 th\u00F4ng b\u00E1o k\u1EBFt qu\u1EA3 theo \u0111\u1ECBa ch\u1EC9 th\u01B0 \u0111i\u1EC7n t\u1EED ")
							  + fileCapCCMGBDS.getEmail()
							  + getText(" ho\u1EB7c s\u1ED1 \u0111i\u1EC7n tho\u1EA1i "));
				  }
				  break;
			  case 8: // Cap lai Chung chi moi gioi BDS
				  fileCapCCMGBDS = fileCapCCMGBDSMgr.get(formId);
				  if(fileCapCCMGBDS!=null){
					  bean.setContent(getText("\u0110\u01A1n xin c\u1EA5p l\u1EA1i ch\u1EE9ng ch\u1EC9 m\u00F4i gi\u1EDBi B\u1EA5t \u0111\u1ED9ng s\u1EA3n")
							  +getText(", m\u00E3 s\u1ED1 ")
							  + fileCapCCMGBDS.getFileCode()
							  + getText(", c\u1EE7a ")
							  + fileCapCCMGBDS.getContactName()
							  + getText(" \u0111\u00E3 \u0111\u01B0\u1EE3c g\u1EDFi \u0111\u1EBFn S\u1EDF x\u00E2y d\u1EF1ng B\u00ECnh D\u01B0\u01A1ng. Ch\u00FAng t\u00F4i s\u1EBD ki\u1EC3m tra h\u1ED3 s\u01A1 v\u00E0 th\u00F4ng b\u00E1o k\u1EBFt qu\u1EA3 theo \u0111\u1ECBa ch\u1EC9 th\u01B0 \u0111i\u1EC7n t\u1EED ")
							  + fileCapCCMGBDS.getEmail()
							  + getText(" ho\u1EB7c s\u1ED1 \u0111i\u1EC7n tho\u1EA1i "));
				  }
				  break;
			  case 9: // Cap moi chung chi dinh gia BDS
				  fileCapCCMGBDS = fileCapCCMGBDSMgr.get(formId);
				  if(fileCapCCMGBDS!=null){
					  bean.setContent(getText("\u0110\u01A1n xin c\u1EA5p m\u1EDBi ch\u1EE9ng ch\u1EC9 \u0111\u1ECBnh gi\u00E1 B\u1EA5t \u0111\u1ED9ng s\u1EA3n")
							  +getText(", m\u00E3 s\u1ED1 ")
							  + fileCapCCMGBDS.getFileCode()
							  + getText(", c\u1EE7a ")
							  + fileCapCCMGBDS.getContactName()
							  + getText(" \u0111\u00E3 \u0111\u01B0\u1EE3c g\u1EDFi \u0111\u1EBFn S\u1EDF x\u00E2y d\u1EF1ng B\u00ECnh D\u01B0\u01A1ng. Ch\u00FAng t\u00F4i s\u1EBD ki\u1EC3m tra h\u1ED3 s\u01A1 v\u00E0 th\u00F4ng b\u00E1o k\u1EBFt qu\u1EA3 theo \u0111\u1ECBa ch\u1EC9 th\u01B0 \u0111i\u1EC7n t\u1EED ")
							  + fileCapCCMGBDS.getEmail()
							  + getText(" ho\u1EB7c s\u1ED1 \u0111i\u1EC7n tho\u1EA1i "));
				  }
				  break;
			  case 10: // cap lai chung chi dinh gia BDS
				  fileCapCCMGBDS = fileCapCCMGBDSMgr.get(formId);
				  if(fileCapCCMGBDS!=null){
					  bean.setContent(getText("\u0110\u01A1n xin c\u1EA5p l\u1EA1ich\u1EE9ng ch\u1EC9 \u0111\u1ECBnh gi\u00E1 B\u1EA5t \u0111\u1ED9ng s\u1EA3n")
							  +getText(", m\u00E3 s\u1ED1 ")
							  + fileCapCCMGBDS.getFileCode()
							  + getText(", c\u1EE7a ")
							  + fileCapCCMGBDS.getContactName()
							  + getText(" \u0111\u00E3 \u0111\u01B0\u1EE3c g\u1EDFi \u0111\u1EBFn S\u1EDF x\u00E2y d\u1EF1ng B\u00ECnh D\u01B0\u01A1ng. Ch\u00FAng t\u00F4i s\u1EBD ki\u1EC3m tra h\u1ED3 s\u01A1 v\u00E0 th\u00F4ng b\u00E1o k\u1EBFt qu\u1EA3 theo \u0111\u1ECBa ch\u1EC9 th\u01B0 \u0111i\u1EC7n t\u1EED ")
							  + fileCapCCMGBDS.getEmail()
							  + getText(" ho\u1EB7c s\u1ED1 \u0111i\u1EC7n tho\u1EA1i "));
				  }
				  break;
			}
		}
		
		wordStream = new BmQT_Notify().toMSWord(getFile("/word/MauThongBao.docx"), bean);
		
		return SUCCESS;
	}

	public String exportSeachFiles() throws IOException, ParseException {
		ServletContext servletContext = ServletActionContext.getServletContext();

		String fromdate = (String)servletContext.getAttribute(WebConstants.SEARCH_FROM);
		String todate = (String)servletContext.getAttribute(WebConstants.SEARCH_TO);
		String department = (String)servletContext.getAttribute(WebConstants.SEARCH_DEPARTMENT);
		String category = (String)servletContext.getAttribute(WebConstants.SEARCH_CATEGORY);
		String status = (String)servletContext.getAttribute(WebConstants.SEARCH_STATUS);
		String file = (String)servletContext.getAttribute(WebConstants.SEARCH_FILEID);
		String field = (String)servletContext.getAttribute(WebConstants.SEARCH_FIELD);
		String submiter = (String)servletContext.getAttribute(WebConstants.SEARCH_SUBMITER);
		
		List<String[]> lstRequest = new ArrayList<String[]>();
		
		String[] fdateTime = {"T\u1EEB ng\u00E0y",fromdate};
		lstRequest.add(fdateTime);
		String[] tdateTime = {"\u0111\u1EBFn ng\u00E0y",todate};
		lstRequest.add(tdateTime);
		String[] arrDepartment = {"Ph\u00F2ng",department};
		lstRequest.add(arrDepartment);
		String[] arrCategory = {"Th\u1EE7 t\u1EE5c",category};
		lstRequest.add(arrCategory);
		String[] arrField = {"L\u0129nh v\u1EF1c",field};
		lstRequest.add(arrField);
		       
		Date f = StringUtils.isEmpty(fromdate) ? null : DateUtils.parseDate(
				fromdate, new String[] { getText("date.format") });
		Date t = StringUtils.isEmpty(todate) ? null : DateUtils.parseDate(
				todate, new String[] { getText("date.format") });
		
		List<org.istt.domain.File> fileList = manager.searchWithoutPagination(NumberUtils.toLong(department), 
				NumberUtils.toLong(status), NumberUtils.toLong(field), NumberUtils.toLong(category), f, t, file, submiter);
		
		wordStream = new ExportFile().toMSWord(getFile("/word/BC_DS_HO_SO_THEO_PHONG.docx"),fileList,lstRequest);
		return SUCCESS;
	}

	public String exportSynthesisReport() throws IOException {
		ServletContext servletContext = ServletActionContext.getServletContext();
		@SuppressWarnings("unchecked")
		List<GeneralReportBean> lstFiles = (List<GeneralReportBean>)servletContext.getAttribute(WebConstants.SERVLET_CONTEXT_REPORT);
		String fromdate = (String)servletContext.getAttribute(WebConstants.SERVLET_CONTEXT_FROMDATE);
		String todate = (String)servletContext.getAttribute(WebConstants.SERVLET_CONTEXT_TODATE);
		String totalInMonth = (String)servletContext.getAttribute(WebConstants.SERVLET_CONTEXT_TOTAL_RECEIVED_INMONTH);
		String totalLastMonth = (String)servletContext.getAttribute(WebConstants.SERVLET_CONTEXT_TOTAL_RECEIVED_FROM_LASTMONTH);
		String reportTitle = (String)servletContext.getAttribute(WebConstants.SERVLET_CONTEXT_REPORT_DEPARTMENTNAME);
		
		wordStream = new ReportSynthesis().toMSWord(getFile("/word/BC_TONG_HOP.docx"),lstFiles ,fromdate,todate,totalInMonth,totalLastMonth,reportTitle);
		return SUCCESS;
	}

	InputStream getFile(String path) throws IOException {
		ServletContext servletContext = ServletActionContext.getServletContext();
	    return new FileInputStream(new File(servletContext.getRealPath(path)));
	}
	
	public FileCapCCMGBDSManager getFileCapCCMGBDSMgr() {
		return fileCapCCMGBDSMgr;
	}
	
	
	private List<Object[]> getFileProcessVoucher(){
		
		List<Object[]> fileProcessVoucher = ((FileManager)manager).getFileProcessVoucher(id);
		
		for (int i = 0; i < fileProcessVoucher.size(); i++) {
			List<Object> tmp = new ArrayList<Object>();
			tmp.addAll(Arrays.asList(fileProcessVoucher.get(i)));
			
			//Dateformat: yyyy-mm-dd hh:mm:ss
			String orFromDate = (String)fileProcessVoucher.get(i)[10];
			String orToDate = (String)fileProcessVoucher.get(i)[11];
			DateTime fromDate = null;
			DateTime toDate = null;
			if(StringUtils.isNotEmpty(orFromDate)){
				fromDate = new DateTime(NumberUtils.toInt(orFromDate.substring(0,4)),
						NumberUtils.toInt(orFromDate.substring(5,7)), 
						NumberUtils.toInt(orFromDate.substring(8,10)),
						NumberUtils.toInt(orFromDate.substring(11,13)),
						NumberUtils.toInt(orFromDate.substring(14,16)));
				
			}

			if(StringUtils.isNotEmpty(orToDate)){
				toDate = new DateTime(NumberUtils.toInt(orToDate.substring(0,4)),
						NumberUtils.toInt(orToDate.substring(5,7)), 
						NumberUtils.toInt(orToDate.substring(8,10)),
						NumberUtils.toInt(orToDate.substring(11,13)),
						NumberUtils.toInt(orToDate.substring(14,16)));
				
			}

			if(fromDate != null && toDate != null){
				System.out.println("....................FROM DATE: "+fromDate + " | TO DATE: "+toDate);
				/*Float totalDay = (Float)tmp.get(0) - getWorkingDays(fromDate, toDate);
				
				if(totalDay < 0.5)
					tmp.set(0, 0.5);
				else if(totalDay > 0.5 && totalDay < 1.0)
					tmp.set(0, 1.0);
				else
					tmp.set(0, new Float(Math.round(totalDay)));*/
				Float totalDay = new Float(0);
				
				Float processTime = (Float)tmp.get(0);
				int workingday = getWorkingDays(fromDate, toDate);
				
				if(processTime > workingday) totalDay = new Float(workingday);
				else totalDay = processTime;
				
				totalDay = new Float(Math.round(totalDay));
				if(totalDay > 0)
					tmp.set(0, totalDay);
				else tmp.set(0, new Float(0));
			}
			
			
			Object[] result = tmp.toArray(new Object[]{});
			fileProcessVoucher.set(i, result);
		}
		
		return fileProcessVoucher;
	}
	
	private TimeWeekendManager weekendManager;
	private TimeHolidayManager holidayManager;
	
	@Autowired
	public void setHolidayManager(TimeHolidayManager holidayManager) {
		this.holidayManager = holidayManager;
	}
	
	@Autowired
	public void setWeekendManager(TimeWeekendManager weekendManager) {
		this.weekendManager = weekendManager;
	}
	
	public int getWorkingDays(DateTime fromdate, DateTime todate) {
		int result = 0;
		
		//period of time
		DateTime start = new DateMidnight().toDateTime();

		if(fromdate!=null) start = fromdate;
		
		//how many holidays are between the period of time 
		List<DateTime> lstDt = new ArrayList<DateTime>();
		List<TimeHoliday> timeHolidays = holidayManager.getList();
		for (TimeHoliday thday : timeHolidays) {
			if (thday.getRepeat()) {
				MutableDateTime mdt = new DateTime(thday.getHolidayDate()).toMutableDateTime();
				mdt.setYear(start.getYear());
				lstDt.add(mdt.toDateTime());
			} else {
				lstDt.add(new DateTime(thday.getHolidayDate()));
			}
		}
		
		//how many days off are between the period of time
		List<TimeWeekend> timeWeekends = weekendManager.getList();
		int[] dow = new int[]{DateTimeConstants.MONDAY, 
								DateTimeConstants.TUESDAY, 
								DateTimeConstants.WEDNESDAY, 
								DateTimeConstants.THURSDAY, 
								DateTimeConstants.FRIDAY, 
								DateTimeConstants.SATURDAY,
								DateTimeConstants.SUNDAY}; 
		List<Integer> dayOffs = new ArrayList<Integer>();
		for (TimeWeekend tw : timeWeekends) {
			for (int i = 0; i < dow.length; i++) {
				if (dow[i] == tw.getDay_id()) {
					dayOffs.add(dow[i]);
				}
			}
		}
		
		Collections.sort(dayOffs);
		
		
		DateTime tmp = start;
		for (;true;) {
			if(todate.getDayOfWeek() == tmp.getDayOfWeek() && todate.getDayOfMonth() == tmp.getDayOfMonth() && todate.getMonthOfYear() == tmp.getMonthOfYear())
				break;
			
			tmp = tmp.plusDays(1);
			System.out.print("tmp = " + tmp.toString("dd/MM/yyyy") + " " + tmp.toString("E"));
			
			boolean dayOffInTheWeek = false;
			boolean dayOffInTheHoliday = false;
			//day of week
			for (Integer dayOff : dayOffs) {
				if (tmp.getDayOfWeek() == dayOff) {
					//System.out.print(" ########## tmp.getDayOfWeek() == dayOff : " + (tmp.getDayOfWeek() == dayOff));
					dayOffInTheWeek = true;
					break;
				}
			}
			
			//day, month
			for (DateTime dt : lstDt) {
				if (dt.getDayOfMonth() == tmp.getDayOfMonth() && dt.getMonthOfYear() == tmp.getMonthOfYear()) {
					dayOffInTheHoliday = true;
				}
			}
			
			if (!dayOffInTheWeek && !dayOffInTheHoliday) result++;
			
 		}
		
		return result;
	}

	public Long getFormId() {
		return formId;
	}

	public String getId() {
		return id;
	}

	public FileManager getManager() {
		return manager;
	}

	private org.istt.domain.File getObjFile(){
		return ((FileManager)manager).get(id);
	}
	
	public Long getType() {
		return type;
	}
	
	String getWordContentType() {
		return documentFormat == "doc" ? "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" : "application/msword";
	}

	public InputStream getWordStream() {
		return wordStream;
	}
	
	@Autowired
	public void setFileBoSungCCHNMgr(FileBoSungCCHNManager fileBoSungCCHNgr) {
		this.fileBoSungCCHNMgr = fileBoSungCCHNgr;
	}

	@Autowired
	public void setFileCapCCHNMgr(FileCapCCHNManager fileCapCCHNMgr) {
		this.fileCapCCHNMgr = fileCapCCHNMgr;
	}

	@Autowired
	public void setFileCapCCMGBDSMgr(FileCapCCMGBDSManager fileCapCCMGBDSMgr) {
		this.fileCapCCMGBDSMgr = fileCapCCMGBDSMgr;
	}

	@Autowired
	public void setFileCapLaiCCHNMgr(FileCapLaiCCHNManager fileCapLaiCCHNMgr) {
		this.fileCapLaiCCHNMgr = fileCapLaiCCHNMgr;
	}

	@Autowired
	public void setFileCapPhepXDMgr(FileCapGPXDManager fileCapPhepXDMgr) {
		this.fileCapPhepXDMgr = fileCapPhepXDMgr;
	}

	@Autowired
	public void setFileDieuChinhGPXDMgr(
			FileDieuChinhGPXDManager fileDieuChinhGPXDMgr) {
		this.fileDieuChinhGPXDMgr = fileDieuChinhGPXDMgr;
	}

	@Autowired
	public void setFileGiaHanCPXDMgr(FileGiaHanGPXDManager fileGiaHanCPXDMgr) {
		this.fileGiaHanCPXDMgr = fileGiaHanCPXDMgr;
	}
	
	public void setFormId(Long formId) {
		this.formId = formId;
	}

	public void setId(String id) {
		this.id = id;
	}

	public void setManager(FileManager manager) {
		this.manager = manager;
	}

	public void setType(Long type) {
		this.type = type;
	}

	public void setFormtype(Integer formtype) {
		this.formtype = formtype;
	}

	public Integer getFormtype() {
		return formtype;
	}
}
